﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using IBS.Bll.Tools;
using IBS.Model;
using IBS.Model.Base;
using IBS.ServiceContracts;

namespace IBS.Bll.Authority
{

    public class RequestValidator
    {
        public const string ERROR_MESSAGE_REQUEST_EXISTS = "User with same email or/and login already exists";
        public const string ERROR_MESSAGE_REQUEST_DOES_NOT_EXIST = "Request does not exist";
        private IIbsContext context;
        private IAdmissionService admissionService;

        public RequestValidator(IIbsContext context, IAdmissionService admissionService)
        {
            this.context = context;
            this.admissionService = admissionService;
        }

        public bool RequestExists(Request request )
        {
            bool sameRequestExists = context.Requests.Count(r => r.Email.ToLower() == request.Email.ToLower()
                                                                 || r.Login == request.Login) > 0;

            bool sameUserExists = admissionService.UserWithSameEmailExists(request.Email) ||
                                   admissionService.UserWithSameNameExists(request.Login);
            return sameRequestExists || sameUserExists;
        }
    }
}
